<template>
    <div class="addRefund-view">

      <modules-title titleName="退费">

        <template slot="other">
          <div class="priceView">
            <div class="priceView-item">
              <span>总金额：¥{{datas.amount.toFixed(2)}}</span>
            </div>

            <div class="priceView-item">
              <span>优惠 ¥{{(datas.amount - datas.receipts_amount).toFixed(2)}}</span>
            </div>

            <div class="priceView-item">
              <span>实收：</span>
              <span class="price">¥{{datas.receipts_amount.toFixed(2)}}</span>
            </div>

            <div class="priceView-item">
              <span>实退：</span>
              <span class="price">¥{{refundAmound.toFixed(2)}}</span>
            </div>


            <div class="priceView-item">
              <span>退费：¥</span>
              <el-input v-model="refundValue" placeholder="请输入" @keyup.native="proving" @blur="inputBlur"></el-input>
            </div>
          </div>
        </template>
        <template slot="btn">
          <el-button class="main-btn" type="primary" @click="refundClick">退费</el-button>
          <el-button class="border-btn" @click="cancelClick">取消</el-button>
        </template>
      </modules-title>


      <div class="scroll-view">
        <div class="payInformation" v-for="(record,index) in datas.charge_record">
          <div class="informationItem">
            <span class="grayTitle" v-if="record.option_type == 1">收费时间：</span>
            <span class="grayTitle" v-else>退费时间：</span>
            <span>{{record.created_at}}</span>
          </div>
          <div class="informationItem">
            <span class="grayTitle" v-if="record.option_type == 1">收费员：</span>
            <span class="grayTitle" v-else>退费员：</span>
            <span>{{record.user_name}}</span>
          </div>

          <div class="informationItem">
            <span class="grayTitle" v-if="record.option_type == 1">实收金额：</span>
            <span class="grayTitle" v-else>实退金额：</span>
            <span>¥{{record.receipts_amount}}</span>
          </div>

          <div class="informationItem">
            <span class="grayTitle" v-if="record.option_type == 1">付款类型：</span>
            <span class="grayTitle" v-else>退款类型：</span>
            <span v-if="record.pay_type == 1">现金</span>
            <span v-else-if="record.pay_type == 2">支付宝</span>
            <span v-else-if="record.pay_type == 3">微信支付</span>
            <span v-else-if="record.pay_type == 4">银行卡</span>
            <span v-else-if="record.pay_type == 5">医保卡</span>
          </div>

        </div>

        <div class="patientView">

          <div class="title">诊断详情</div>

          <div class="bese-information">
            <div class="information-item">
              <span class="grayTitle">患者：</span>
              <span class="value">{{datas.real_name}}</span>
            </div>

            <div class="information-item">
              <span class="grayTitle">性别：</span>
              <span class="value" v-if="datas.gender == '0'">男</span>
              <span class="value" v-else-if="datas.gender == 1">女</span>
            </div>

            <div class="information-item">
              <span class="grayTitle">年龄：</span>
              <span class="value">{{datas.age | dealAge}}</span>
            </div>

            <div class="information-item" v-if="datas.doctor_name && datas.doctor_name != ''">
              <span class="grayTitle">医生：</span>
              <span class="value">{{datas.doctor_name}}</span>
            </div>

            <div class="information-item" v-if="datas.department_name && datas.department_name != ''">
              <span class="grayTitle">科室：</span>
              <span class="value">{{datas.department_name}}</span>
            </div>

            <div class="information-item">
              <span class="grayTitle">就诊日期：</span>
              <span class="value">{{datas.created_at}}</span>
            </div>

            <div class="information-item">
              <span class="grayTitle">就诊类型：</span>
              <span class="value" v-if="datas.medical_record_repeat == 2">复诊</span>
              <span class="value" v-else-if="datas.medical_record_repeat == 1">初诊</span>
              <span class="value" v-else>无</span>
            </div>
          </div>

          <div class="total-view">
            <div class="information-item">
              <span class="grayTitle">诊断：</span>
              <span class="value">{{datas.result}}</span>
            </div>
          </div>
        </div>

<!--        <div class="patientView" v-for="(item,index) in datas.refund_content">-->
<!--          <div class="title" v-if="item.pay_type == 1">现金支付</div>-->
<!--          <div class="title" v-else-if="item.pay_type == 2">支付宝支付</div>-->
<!--          <div class="title" v-else-if="item.pay_type == 3">微信支付</div>-->
<!--          <check-item v-if="item.check_item  && item.check_item.length > 0" :datas="item.check_item" :type="charge_no? item.charge_no == charge_no? 2 : pay_type == 1 ? pay_type == item.pay_type ? 2 : 4 : 4 :2" @dataChange="(list)=>{checkChange(item.pay_type,item.charge_no,list)}"></check-item>-->

<!--          <surcharge-item v-if="item.surcharge  && item.surcharge.length > 0" :datas="item.surcharge" :type="charge_no? item.charge_no == charge_no? 2 : pay_type == 1 ? pay_type == item.pay_type ? 2 : 4 : 4 :2" @dataChange="(list)=>{surchargeChange(item.pay_type,item.charge_no,list)}"></surcharge-item>-->

<!--          <prescription-item v-for="i in item.prescription_ary_info" :method="datas.sell_method" v-model="i.details" :data="i" :type="charge_no? item.charge_no == charge_no? 2 : pay_type == 1 ? pay_type == item.pay_type ? 2 : 4 : 4 :2" @dataChange="(list)=>{prescriptionChange(item.pay_type,item.charge_no,list)}"></prescription-item>-->
<!--        </div>-->

        <check-item v-if="datas.check_item  && datas.check_item.length > 0" :datas="datas.check_item" :type="type" @dataChange="checkChange"></check-item>

        <surcharge-item v-if="datas.surcharge  && datas.surcharge.length > 0" :datas="datas.surcharge" :type="type" @dataChange="surchargeChange"></surcharge-item>

        <prescription-item v-for="i in datas.prescription_ary_info" :method="datas.sell_method" v-model="i.details" :data="i" :type="type" @dataChange="prescriptionChange"></prescription-item>


      </div>

      <refund-dialog ref="refundDialog" @refund-confirm="refundConfirm"></refund-dialog>

    </div>
</template>

<script>
  import surchargeItem from "../modules/surchargeItem";
  import checkItem from "../modules/checkItem";
  import {medicineCharge} from "../../../api/payOrDrug";
  import {medicineRefundDetail} from "../../../api/payOrDrug";
  import refundDialog from "../modules/refundDialog";
    import prescriptionItem from "../modules/prescriptionItem";

    export default {
        name: "index",
        components:{prescriptionItem,refundDialog,checkItem,surchargeItem},
        data:function () {
            return{
                type:2,
                value:'',
                checked:false,

                refundValue:0,
                datas:{
                    receipts_amount:0,
                    amount:0,
                    created_at:'',
                    age:'',
                },
                refundDatas:{},
                pay_type:0,
                charge_no:0,
                refundAmound:0,
            }
        },
        mounted:function(){
            if (this.$route.query.id){
                this.getData(this.$route.query.id)
            }
        },
        methods:{
            getType:function(){
                return this.type
            },
            refundConfirm:function(data){
                this.refundDatas.id = this.refundDatas.charge_id
                this.refundDatas.option_type = 2;
                this.refundDatas.pay_type = 1
                this.refundDatas.remark = data.remark
                this.refundDatas.receipts_amount = Number(this.refundValue)

                // this.refundDatas.prescription_ary_info = [];
                // this.refundDatas.surcharge = [];
                // this.refundDatas.check_item = [];

                let num = 0;
                let price = 0;
                // for (let j = 0; j < this.refundDatas.refund_content.length; j++){
                //
                //     let refund = this.refundDatas.refund_content[j]
                //     let list = []
                //     for (let i = 0; i < refund.prescription_ary_info.length; i++){
                //         let data = refund.prescription_ary_info[i];
                //
                //         if (data.details.length > 0){
                //             for (let j = 0; j < data.details.length; j++){
                //                 if (data.details[j].number == Number(data.details[j].refundNum)){
                //                     data.details[j].status = 0;
                //                 }else {
                //                     data.details[j].status = 1;
                //                 }
                //                 data.details[j].number = Number(data.details[j].refundNum)
                //                 let amount = data.details[j].refundNum*data.details[j].price;
                //                 data.details[j].amount = amount;
                //                 num += data.details[j].refundNum;
                //                 price += amount;
                //             }
                //             list.push(data)
                //         }
                //     }
                //     refund.prescription_ary_info = list;
                //
                //     for (let i = 0; i < refund.surcharge.length; i++){
                //         num ++;
                //         price += refund.surcharge[i].amount;
                //     }
                //
                //     for (let i = 0; i < refund.check_item.length; i++){
                //         refund.check_item[i].check_item_status = 2;
                //         refund.check_item[i].check_item_number = Number(refund.check_item[i].refundNum);
                //         refund.check_item[i].amount = refund.check_item[i].check_item_number*refund.check_item[i].price;
                //         num += refund.check_item[i].check_item_number;
                //         price += refund.check_item[i].check_item_number*refund.check_item[i].price;
                //     }
                //
                //     if (refund.prescription_ary_info.length > 0){
                //         this.refundDatas.prescription_ary_info = this.refundDatas.prescription_ary_info.concat(refund.prescription_ary_info)
                //     }
                //     this.refundDatas.surcharge = this.refundDatas.surcharge.concat(refund.surcharge)
                //     this.refundDatas.check_item = this.refundDatas.check_item.concat(refund.check_item)
                // }
                // this.refundDatas.refund_content = null;


                let refund = this.refundDatas;
                let list = []
                for (let i = 0; i < refund.prescription_ary_info.length; i++){
                    let data = refund.prescription_ary_info[i];

                    if (data.details.length > 0){
                        for (let j = 0; j < data.details.length; j++){
                            if (data.details[j].number == Number(data.details[j].refundNum)){
                                data.details[j].status = 0;
                            }else {
                                data.details[j].status = 1;
                            }
                            data.details[j].number = Number(data.details[j].refundNum)
                            let amount = data.details[j].refundNum*data.details[j].price;
                            data.details[j].amount = amount;
                            num += data.details[j].refundNum;
                            price += amount;
                        }
                        list.push(data)
                    }
                }
                refund.prescription_ary_info = list;

                for (let i = 0; i < refund.surcharge.length; i++){
                    num ++;
                    price += refund.surcharge[i].amount;
                }

                for (let i = 0; i < refund.check_item.length; i++){
                    refund.check_item[i].check_item_status = 2;
                    refund.check_item[i].check_item_number = Number(refund.check_item[i].refundNum);
                    refund.check_item[i].amount = refund.check_item[i].check_item_number*refund.check_item[i].price;
                    num += refund.check_item[i].check_item_number;
                    price += refund.check_item[i].check_item_number*refund.check_item[i].price;
                }

                let num2 = 0;
                let datas = this.datas

                for (let i = 0; i < datas.prescription_ary_info.length; i++){
                    let data = datas.prescription_ary_info[i];

                    for (let j = 0; j < data.details.length; j++){
                        num2 += data.details[j].number;
                    }
                }

                if (datas.check_item){
                    for (let i = 0; i < datas.check_item.length; i++){
                        num2 += datas.check_item[i].check_item_number;
                    }
                }

                if (datas.surcharge){
                    for (let i = 0; i < datas.surcharge.length; i++){
                        num2 ++;
                    }
                }
                // for (let j = 0; j < this.datas.refund_content.length; j++){
                //     let datas = this.datas.refund_content[j]
                //
                //     for (let i = 0; i < datas.prescription_ary_info.length; i++){
                //         let data = datas.prescription_ary_info[i];
                //
                //         for (let j = 0; j < data.details.length; j++){
                //             num2 += data.details[j].number;
                //         }
                //     }
                //
                //     if (datas.check_item){
                //         for (let i = 0; i < datas.check_item.length; i++){
                //             num2 += datas.check_item[i].check_item_number;
                //         }
                //     }
                //
                //     if (datas.surcharge){
                //         for (let i = 0; i < datas.surcharge.length; i++){
                //             num2 ++;
                //         }
                //     }
                // }

                this.refundDatas.is_finish_refund = num == num2 ? 2 : 1;

                this.refundDatas.amount = price;

                medicineCharge(this.refundDatas).then((res)=>{
                    if (res.code == 200){
                        this.$message({
                            message: '退款成功',
                            type: 'success'
                        });
                        this.$router.replace({name:'refundList',query:{t:new Date().getTime()}});
                    }else {
                        this.$message.error(res.msg);
                        this.$router.back();
                    }
                })
            },
            refundClick:function () {
                console.log(this.list);
                let refund = false;
                for (let i = 0; i < this.refundDatas.prescription_ary_info.length; i++){
                    let data = this.refundDatas.prescription_ary_info[i];

                    for (let j = 0; j < data.details.length; j++){
                        refund = true;
                    }
                }
                for (let i = 0; i < this.refundDatas.surcharge.length; i++){
                    refund = true;
                }
                for (let i = 0; i < this.refundDatas.check_item.length; i++){
                    refund = true;
                }
                if (!refund){
                    this.$message.error("请选择退款对象");
                    return;
                }
                this.$refs.refundDialog.openDialog(this.refundValue)
            },
            cancelClick:function () {
                this.$router.back();
            },
            getData:function (id) {
                medicineRefundDetail(id).then((res)=>{
                    if (res.code == 200){

                        // for (let j = 0; j < res.data.refund_content.length; j++){
                        //     let content = res.data.refund_content[j];
                        //     for (let i = 0; i < content.prescription_ary_info.length; i++){
                        //         let data = content.prescription_ary_info[i]
                        //         for (let j = 0; j < data.details.length; j++){
                        //             let a = JSON.stringify(data.details[j].number)
                        //             data.details[j].refundNum = JSON.parse(a);
                        //         }
                        //     }
                        //     if (content.check_item){
                        //         for (let i = 0; i < content.check_item.length; i++){
                        //             let data = content.check_item[i]
                        //             let a = JSON.stringify(data.check_item_number)
                        //             data.refundNum = JSON.parse(a);
                        //         }
                        //     }
                        // }
                        let content = res.data
                        for (let i = 0; i < content.prescription_ary_info.length; i++){
                            let data = content.prescription_ary_info[i]
                            for (let j = 0; j < data.details.length; j++){
                                let a = JSON.stringify(data.details[j].number)
                                data.details[j].refundNum = JSON.parse(a);
                            }
                        }
                        if (content.check_item){
                            for (let i = 0; i < content.check_item.length; i++){
                                let data = content.check_item[i]
                                let a = JSON.stringify(data.check_item_number)
                                data.refundNum = JSON.parse(a);
                            }
                        }

                        this.refundAmound = 0;
                        for (let i = 0; i < content.charge_record.length; i++){
                            let rec = content.charge_record[i];
                            if (rec.option_type != 1){
                                this.refundAmound += rec.receipts_amount
                            }
                        }


                        this.datas = res.data;
                        this.refundDatas = {...this.datas}
                        // this.refundDatas.refund_content = [];
                        this.refundDatas.prescription_ary_info = [];
                        this.refundDatas.check_item = [];
                        this.refundDatas.surcharge = [];
                    }else {
                        this.$message.error(res.msg);
                    }
                })
            },
            sss:function(refund){

                let num = 0;
                let price = 0;
                for (let i = 0; i < refund.prescription_ary_info.length; i++){
                    let data = refund.prescription_ary_info[i];

                    for (let j = 0; j < data.details.length; j++){
                        num += data.details[j].refundNum;
                        price += data.details[j].refundNum*data.details[j].price;
                    }
                }

                for (let i = 0; i < refund.surcharge.length; i++){
                    num ++;
                    price += refund.surcharge[i].amount;
                }

                for (let i = 0; i < refund.check_item.length; i++){
                    num += refund.check_item[i].refundNum;
                    price += refund.check_item[i].refundNum*refund.check_item[i].price;
                }
                return {num:num,price:price}
            },
            prescriptionChange:function (list) {
                // this.pay_type = type;
                // this.charge_no = no;
                //
                // let a = this.refundDatas.refund_content.findIndex(a=>a.charge_no == this.charge_no)
                // if (a != -1){
                //     let index = -1;
                //     for (let i = 0; i < this.refundDatas.refund_content[a].prescription_ary_info.length; i++){
                //         if (this.refundDatas.refund_content[a].prescription_ary_info[i].id == list.id){
                //             index = i;
                //         }
                //     }
                //     if (index != -1){
                //         this.refundDatas.refund_content[a].prescription_ary_info[index] = list;
                //     }else {
                //         this.refundDatas.refund_content[a].prescription_ary_info.push(list);
                //     }
                //     if (!this.sss(this.refundDatas.refund_content[a]).num){
                //         this.pay_type = 0;
                //         this.charge_no = 0;
                //     }
                // }else {
                //     let data = {
                //         pay_type:type,
                //         charge_no:no,
                //         prescription_ary_info:[list],
                //         check_item:[],
                //         surcharge:[],
                //     }
                //     this.refundDatas.refund_content.push(data);
                //     if (!this.sss(this.refundDatas.refund_content[this.refundDatas.refund_content.length-1]).num){
                //         this.pay_type = 0;
                //         this.charge_no = 0;
                //     }
                // }

                let index = -1;
                for (let i = 0; i < this.refundDatas.prescription_ary_info.length; i++){
                    if (this.refundDatas.prescription_ary_info[i].id == list.id){
                        index = i;
                    }
                }
                if (index != -1){
                    this.refundDatas.prescription_ary_info[index] = list;
                }else {
                    this.refundDatas.prescription_ary_info.push(list);
                }

                this.refundValue = 0;
                // this.totalChange();
            },
            checkChange:function(list){
                // this.pay_type = type;
                // this.charge_no = no;
                //
                // let a = this.refundDatas.refund_content.findIndex(a=>a.charge_no == this.charge_no)
                // if (a != -1){
                //     this.refundDatas.refund_content[a].check_item = list;
                //
                //     if (!this.sss(this.refundDatas.refund_content[a]).num){
                //         this.pay_type = 0;
                //         this.charge_no = 0;
                //     }
                // }else {
                //     let data = {
                //         pay_type:type,
                //         charge_no:no,
                //         prescription_ary_info:[],
                //         check_item:list,
                //         surcharge:[],
                //     }
                //     this.refundDatas.refund_content.push(data);
                //
                //     if (!this.sss(this.refundDatas.refund_content[this.refundDatas.refund_content.length-1]).num){
                //         this.pay_type = 0;
                //         this.charge_no = 0;
                //     }
                // }
                this.refundDatas.check_item = list;
                // console.log(this.refundDatas)
                // this.totalChange();
                this.refundValue = 0;
            },
            surchargeChange:function(list){
                // this.pay_type = type;
                // this.charge_no = no;
                //
                // let a = this.refundDatas.refund_content.findIndex(a=>a.charge_no == this.charge_no)
                // if (a != -1){
                //     this.refundDatas.refund_content[a].surcharge = list;
                //
                //     if (!this.sss(this.refundDatas.refund_content[a]).num){
                //         this.pay_type = 0;
                //         this.charge_no = 0;
                //     }
                // }else {
                //     let data = {
                //         pay_type:type,
                //         charge_no:no,
                //         prescription_ary_info:[],
                //         check_item:[],
                //         surcharge:list,
                //     }
                //     this.refundDatas.refund_content.push(data);
                //
                //     if (!this.sss(this.refundDatas.refund_content[this.refundDatas.refund_content.length-1]).num){
                //         this.pay_type = 0;
                //         this.charge_no = 0;
                //     }
                // }
                this.refundDatas.surcharge = list;
                // console.log(this.refundDatas)
                this.refundValue = 0;
                // this.totalChange();
            },
            totalChange:function(){
                let num = 0;
                let price = this.sss(this.refundDatas).price;
                num+=price

                this.refundValue = num;
            },
            inputBlur:function(){
                this.refundValue = Number(this.refundValue).toFixed(2)
            },
            proving:function() {
                let str = this.refundValue
                // this.form.skus[e].Price 是input的值　　
                // 先把非数字的都替换掉，除了数字和.
                str = str.replace(/[^\d.]/g, '');
                // 必须保证第一个为数字而不是.
                str = str.replace(/^\./g, '');
                // 保证只有出现一个.而没有多个.
                str = str.replace(/\.{2,}/g, '');
                // 保证.只出现一次，而不能出现两次以上
                str = str.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
                let index = -1
                for (let i in str) {
                    if (str[i] === '.') {
                        index = i
                    }
                    if (index !== -1) {
                        if (i - index > 2) {
                            str = str.substring(0, str.length - 1)
                        }
                    }
                }
                if (Number(str) > (this.refundDatas.receipts_amount - this.refundAmound).toFixed(2)){
                    str = (this.refundDatas.receipts_amount - this.refundAmound).toFixed(2)
                }
                this.refundValue = str;

            },
        }
    }
</script>

<style lang="scss" scoped>
  @import "@/assets/scss/payOrDrug/addRefund.scss";
</style>
